<?php
namespace app\api\controller;
use app\common\controller\Base;
use app\common\exception\FailException;
use app\common\exception\ParameterException;
use app\common\logic\Order as OrderLogic;
use think\Request;
use think\Loader;

/**
 * @title 订单模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */
class Order extends Base
{
    /**
     * @title 获取订单列表
     * @description 接口说明
     * @author WCJ
     * @url /api/order/getOrderList
     * @method POST
     * @param name:start_time type:int require:0 default:1 other: desc:开始时间
     * @param name:end_time type:int require:0 default:20 other: desc:结束时间
     * @param name:start type:int require:0 default:1 other: desc:起止数
     * @param name:num type:int require:0 default:20 other: desc:个数
     * @param name:evaluate type:int require:0 default:20 other: desc:1.待评价，2已评价
     * @param name:type type:int require:0 default:20 other: desc:1.为余额2.现金支付3.支付宝4.微信5.银行卡
     *
     * @return data:订单数据
     *
     *
     */
     public function getOrderList(Request $request){
         if($request->isPost()) {
             $userid = $this->uid;
             $start_time = $request->post("start_time");
             $end_time = $request->post("end_time");
             $start = $request->post('start') ? $request->post('start') : 1;
             $num = $request->post('num') ? $request->post('num') : 20;
             $evaluate=$request->post("evaluate");
             $validate = Loader::validate("Common");
             $type=$request->post("type");
             if (!$validate->scene("date")->check(['date' => $start_time])) {

                 $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                 throw new ParameterException($ret);

             }
             if (!$validate->scene("date")->check(['date' => $end_time])) {

                 $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                 throw new ParameterException($ret);

             }

              if (!$validate->scene("common")->check(['id' => $evaluate])) {

                  $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                  throw new ParameterException($ret);

              }

             if (!$validate->scene("common")->check(['id' => $type])) {

                 $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                 throw new ParameterException($ret);

             }


              $orderlogic=new OrderLogic();
              $data=$orderlogic->get_list($userid,$start_time,$end_time,$start,$num,$evaluate,$type);

             $datas= ["code"=>200,"msg"=>"成功","data"=>$data[0],"count"=>$data[1],"sum"=>$data[2]];

             if($this->new_token){
                 $datas["new_token"]=$this->new_token;
             }
             return $datas;


         }
     }


    /**
     * @title 获取商家订单列表
     * @description 接口说明
     * @author WCJ
     * @url /api/order/getOrderList
     * @method POST
     * @param name:shopid type:int require:0 default:1 other: desc:商家ID
     * @param name:start_time type:int require:0 default:1 other: desc:开始时间
     * @param name:end_time type:int require:0 default:20 other: desc:结束时间
     * @param name:start type:int require:0 default:1 other: desc:起止数
     * @param name:num type:int require:0 default:20 other: desc:个数
     * @param name:evaluate type:int require:0 default:20 other: desc:1.待评价，2已评价
     * @param name:type type:int require:0 default:20 other: desc:1.为余额2.现金支付3.支付宝4.微信5.银行卡
     *
     * @return data:订单数据
     *
     *
     */
    public function getShopOrderList(Request $request){
        if($request->isPost()) {
            $shopid = $this->uid;
            $usertype=$this->type;
            if($usertype!=2){
                throw new FailException(["code"=>201,"msg"=>"无权限访问"]);
            }
            $start_time = $request->post("start_time");
            $end_time = $request->post("end_time");
            $start = $request->post('start') ? $request->post('start') : 1;
            $num = $request->post('num') ? $request->post('num') : 20;
            $evaluate=$request->post("evaluate");
            $number=$request->post("number");

            $validate = Loader::validate("Common");
            $type=$request->post("type");
            if (!$validate->scene("date")->check(['date' => $start_time])) {

                $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                throw new ParameterException($ret);

            }
            if (!$validate->scene("date")->check(['date' => $end_time])) {

                $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                throw new ParameterException($ret);

            }

            if (!$validate->scene("common")->check(['id' => $evaluate])) {

                $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                throw new ParameterException($ret);

            }

            if (!$validate->scene("common")->check(['id' => $number])) {

                $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                throw new ParameterException($ret);

            }

            if (!$validate->scene("shopid")->check(['shopid' => $shopid])) {

                $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                throw new ParameterException($ret);

            }


            $orderlogic=new OrderLogic();
            $data=$orderlogic->get_shop_list($shopid,$start_time,$end_time,$start,$num,$evaluate,$type,$number);

            $datas= ["code"=>200,"msg"=>"成功","data"=>$data[0],"count"=>$data[1],"sum"=>$data[2]];

            if($this->new_token){
                $datas["new_token"]=$this->new_token;
            }
            return $datas;


        }
    }

    /**
     * @title 获取订单详情
     * @description 接口说明
     * @author WCJ
     * @url /api/order/getOrderdt
     * @method POST
     * @param name:id type:int require:0 default:1 other: desc:OrderId
     *
     * @return data:订单数据
     *
     *
     */
    public function getOrderdt(Request $request){
        if($request->isPost()) {
            $userid = $this->uid;
            $id = $request->post("id");

            $validate = Loader::validate("Common");


            if (!$validate->scene("common")->check(['id' => $id])) {

                $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode'=>303];
                throw new ParameterException($ret);

            }


            $orderlogic=new OrderLogic();
            $data=$orderlogic->get_info($userid,$id);

            $datas= ["code"=>200,"msg"=>"成功","data"=>$data];

            if($this->new_token){
                $datas["new_token"]=$this->new_token;
            }
            return $datas;


        }
    }
}